"""
tuple元组的定义和操作
    元组不可被修改(元组嵌套的list是特例)
"""
# 定义元组
t1 = (20, "hello", True)
t2 = ()
t3 = tuple()
print(f"元组t1的类型是:{type(t1)},内容是:{t1}")
print(f"元组t2的类型是:{type(t2)},内容是:{t2}")
print(f"元组t3的类型是:{type(t3)},内容是:{t3}")

# 定义单个元素，必须在后面加上,
t4 = ("hello",)
print(f"元组t4的类型是:{type(t4)},内容是:{t4}")

# 元组的嵌套
t5 = ((1, 2, 3), (4, 5, 6))
print(f"元组t5的类型是:{type(t5)},内容是:{t5}")

# 通过元组的下标索引取出元素
num = t5[1][2]
print(f"嵌套元组取出的数据为:{num}")

print("----------------------------------")
# 元组的操作 index count len

# index查找方法
t6 = ("a", "b", "c")
index_a = t6.index("a")
print(f"元组t6中元素a的下标索引为:{index_a}")

# count统计元组中元素的数量
t7 = ("a", "b", "c", "a", "a")
count_a = t7.count("a")
print(f"元组t7中元素a的个数为:{count_a}")

# len(tuple) 元组的长度
len_7 = len(t7)
print(f"元组t7的长度为：{len_7}")

# 元组的遍历 while
print("----------------------------------")
t8 = ("a", "b", "c", "a", "a")


def tuple_while_func(my_tuple):
    """
    while循环遍历元组
    :param my_tuple: 元组参数
    :return:
    """
    index = 0
    while index < len(my_tuple):
        element = my_tuple[index]
        print(f"元组下标{index}位置的元素为：{element}")
        # 循环条件递增1
        index += 1


tuple_while_func(t8)


# 元组的遍历 for

def tuple_for_func(my_tuple):
    """
    for循环遍历元组
    :param my_tuple: 元组参数
    :return:
    """
    for element in my_tuple:
        print(f"取出元组中的元素:{element}")


tuple_for_func(t8)

# 修改元组内容 元组不可修改
t9 = (1, 2, 3)
# t9[0] = 4  # 'tuple' object does not support item assignment

# 特例：元组中嵌套的list可被修改
t10 = (1, 2, [1, 2])
print(f"修改前的元组t10为:{t10}")
t10[2][0] = "a"
t10[2][1] = "b"
print(f"修改后的元组t10为:{t10}")
